#!/usr/bin/env python

#    r,g,b values are from 0 to 1
#    h = [0,360], s = [0,1], v = [0,1]
#    if s == 0, then h = -1 (undefined)




def HSVtoRGB( h, s, v):
    if not s:
        #achromatic (grey)
        return v,v,v
        # sector 0 to 5
    h /= 60.0
    i = int ( h );
    #factorial part of h
    f = h - i
    p = v * ( 1.0 - s )
    q = v * ( 1.0 - s * f )
    t = v * ( 1.0 - s * ( 1.0 - f ) )
    return {
              0: (v,t,p),
              1: (q,v,p),
              2: (p,v,t),
              3: (p,q,v),
              4: (t,p,v),
              5: (v,p,q),
            }[i]

def get_contrasty_colours ():
    """
    returns list of contrasty colours around hsv colour wheel
    Uses RGB(255:255:255) format
    """
    contrasty_colours = list()
    for h in xrange(0, 1080, 30):
        v = 1.0 - (((h /360) * 0.50) % 1.0)
        s = 1.0 - (((h /720) * 0.50) % 1.0)
        rgb = map(lambda x: int(x * 255), HSVtoRGB(h % 360,  s,  v))
        contrasty_colours.append(rgb)
    contrasty_colours[11:12]= [contrasty_colours[12],
                               [0,0,0],
                               [128,128,128],
                               [192,192,192]]
    return contrasty_colours

def get_dark_contrasty_colours ():
    """
    returns list of contrasty colours around hsv colour wheel
    Uses RGB(255:255:255) format
    """
    contrasty_colours = list()
    for h in xrange(0, 1080, 60):
        v = (((h /720) * 0.50) % 1.0) + 0.5
        s = 1.0 - (((h /360) * 0.50) % 1.0)
        rgb = map(lambda x: int(x * 255), HSVtoRGB(h % 360,  s,  v))
        contrasty_colours.append(rgb)
    contrasty_colours[11:12]= [contrasty_colours[12],
                               [0,0,0],
                               [128,128,128],
                               [192,192,192]]
    return contrasty_colours

import unittest, os,sys
if __name__ == '__main__':
    exe_path = os.path.split(os.path.abspath(sys.argv[0]))[0]
    sys.path.append(os.path.abspath(os.path.join(exe_path,"..", "python_modules")))
    from SVGdraw import *
    import SVGdraw

class TestCommonFunctions(unittest.TestCase):

    #       self.assertEqual(self.seq, range(10))
    #       self.assert_(element in self.seq)
    #       self.assertRaises(ValueError, random.sample, self.seq, 20)



    def test_HSVtoRGB(self):
        """
            test a range of different colours
        """
        #
        # create drawing then you create a SVG root element
        #
        drawing=SVGdraw.drawing()
        svg_root=svg()


        # svg root discription
        paper_width  = 297.0  # in mm
        paper_height = 210.0  # in mm
        paper_margin = 20
        drawing_width  = paper_width - paper_margin * 2.0
        drawing_height = paper_height - paper_margin * 2.0
        drawing_offset = paper_margin

        svg_root.attributes["height"]    = str(paper_height) + "mm"
        svg_root.attributes["width"]     = str(paper_width) + "mm"
        svg_root.attributes["viewBox"] = "0 0 %d %d" % (paper_width, paper_height)

        contrasty_colours =  get_contrasty_colours()

        y_pos = 0
        x_pos = 0
        colour_box_height = 10
        colour_box_width  = 55
        for i, rgb in enumerate(contrasty_colours):
            fill_colour = "rgb(%d,%d,%d)" % tuple(rgb)
            x_pos = (i - 3) / 12 * colour_box_width if i >= 15 else 0
            y_pos = (i - 3) % 12 * colour_box_height if i >= 15 else i * colour_box_height
            svg_root.addElement(rect(paper_margin + x_pos, int(paper_margin + y_pos),
                                colour_box_width - 5, colour_box_height - 1, fill = fill_colour))
        drawing.setSVG(svg_root)
        open(os.path.join(exe_path,  "test", "test_colours.svg"), "w").write(drawing.toXml())

#
#   debug code not run if called as a module
#
if __name__ == '__main__':
    unittest.main()

